package com.zs.games.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 登陆拦截器
 * 
 * @date 2023年05月10日 09:00
 */
@Configuration
public class LoginWebMvcConfig implements WebMvcConfigurer {

    @Value("${upload.path}")
    private String uploadPath;
    @Value("${upload.mapping}")
    private String fileMapping;

    @Bean
    UserLoginInterceptor userLoginInterceprator() {
        return new UserLoginInterceptor();
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(userLoginInterceprator())
                .addPathPatterns("/**")
                //放行资源
                .excludePathPatterns("/login","/register","/static","/webjars/**","/swagger-resources/**","/doc.html","/v3/api-docs","/favicon.ico")
                .excludePathPatterns("/upload/**","/file/**")
                .excludePathPatterns("/game/pageList","/game/info","/news/pageList","/news/getInfo");

        WebMvcConfigurer.super.addInterceptors(registry);
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        // 文件上传路径:把上传文件地址file:E:/upload/  映射为 /images/**
        registry.addResourceHandler(fileMapping + "**")  // /images/**
                .addResourceLocations("file:"+ uploadPath);  // file:E:/upload/

    }
}
